#/bin/bash
##配置各种基于yum管理器的源
mkdir -p /etc/yum.repos.d/
#源站选择
source_yum_mirrors(){
	#1参传入新的镜像
	#2参传入默认镜像站
	#3参传入文件名
	if [[ -n "$1" ]];then
		sed -i "s#$2#$1#g" /etc/yum.repos.d/$3
		unset URL
	fi
}

##源缓存建立
makecache(){
	yum clean all 
	yum makecache
	chek_status "$?" "$1"
	if [[ ! -e '/yum_base.log' ]];then
		service_install 'yum-utils device-mapper-persistent-data lvm2' > /dev/null
	fi
	echo 'yum_base=y' > /yum_base.log
}
#源配置文件备份检测
source_yum_bak(){
	if [[ -d "/yum_bak/" ]];then
		green_zt "源文件已备份"
		rm -f /etc/yum.repos.d/*
	else
		green_zt "正在备份源文件"
		mkdir -p /yum_bak/
		mv /etc/yum.repos.d/* /yum_bak/
		red_z_green_z_info "源备份" "[SCUUESS]"
	fi
}
##openEuler系统源配置
source_yum_openEuler20.03(){
echo '[openEuler-source]
name=openEuler-source
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/source/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/source/RPM-GPG-KEY-openEuler

[openEuler-os]
name=openEuler-os
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/OS/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/OS/x86_64/RPM-GPG-KEY-openEuler

[openEuler-everything]
name=openEuler-everything
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/everything/x86_64/RPM-GPG-KEY-openEuler

[openEuler-EPOL]
name=openEuler-epol
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.03-LTS/EPOL/x86_64/
enabled=1
gpgcheck=0
' > /etc/yum.repos.d/openEulerOS.repo 
}
source_yum_openEuler20.09(){
echo '[openEuler-source]
name=openEuler-source
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/source/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/source/RPM-GPG-KEY-openEuler

[openEuler-os]
name=openEuler-os
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/OS/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/OS/x86_64/RPM-GPG-KEY-openEuler

[openEuler-everything]
name=openEuler-everything
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/everything/x86_64/RPM-GPG-KEY-openEuler

[openEuler-EPOL]
name=openEuler-epol
baseurl=https://mirrors.huaweicloud.com/openeuler/openEuler-20.09/EPOL/x86_64/
enabled=1
gpgcheck=0
' > /etc/yum.repos.d/openEulerOS.repo 
}
#已确定可用(CentOS7-Base源)
source_yum_centos7(){
echo '[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/os/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/updates/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras 
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7' > /etc/yum.repos.d/CentOS-Base.repo
	chek_status "$?" "centos7系统源配置文件写入"
	##判断是否通过1参传入镜像站替换
	source_yum_mirrors "$mirrors_os" "https://mirrors.huaweicloud.com" "CentOS-Base.repo"
	makecache "centos7系统源配置"

}
source_yum_centos8(){
echo '
[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
[PowerTools]
name=CentOS-$releasever - PowerTools
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-$releasever - AppStream
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official' > /etc/yum.repos.d/CentOS-Base.repo
	chek_status "$?" "centos8系统源配置文件写入"
	##判断是否通过1参传入镜像站替换
	makecache "系统源配置"
}
#已确定可用(docker-ce源)
source_yum_docker-ce(){
echo '[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.huaweicloud.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/docker-ce/linux/centos/gpg
' >  /etc/yum.repos.d/Docker.repo
	##判断是否通过1参传入镜像站替换
	source_yum_mirrors "$mirrors_docker"  "https://mirrors.huaweicloud.com" "Docker.repo"
	if [[ "$System_Type" == 'openEuler' ]];then
		green_zt "使用默认Docker版本"
	else
		sed -i "s#/7/#/${System_version}/#g" /etc/yum.repos.d/Docker.repo
	fi
	makecache "Dcoker源配置"
}
##已确定可用(K8S源)
source_yum_k8s(){
echo '[kubernetes]
name=Kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0' > /etc/yum.repos.d/kubernetes.repo
	source_yum_mirrors "$URL" "https://mirrors.tuna.tsinghua.edu.cn" "kubernetes.repo"
	makecache "kubernetes源索引建立"
}
#已确定可用（epel7源）
source_yum__epel7(){
echo '[epel]
name=epel - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0' > /etc/yum.repos.d/epel7.repo
	chek_status "$?" "epel7源配置写入"
	##判断是否通过1参传入镜像站替换
	source_yum_mirrors "https://mirrors.ustc.edu.cn" "epel7.repo"
	makecache  "epel7源获取"
}
#已确定可用
source_yum__epel7_testing(){
cat <<EOF > /etc/yum.repos.d/epel-testing.repo
[epel-testing]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
EOF
	makecache "清华epel7源配置"
}
##待确定语法
source_yum__epel8(){
	yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
	sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
	sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
	makecache "epel8源获取"
}
source_yum__REMI(){
	rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
	rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-${source_version}.rpm
	makecache "REMI源获取"
}
source_yum__RPM_Fusion(){
	yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-${source_version}.noarch.rpm -y
	yum localinstall --nogpgcheck https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-${source_version}.noarch.rpm -y
	makecache "RPM_Fusion源获取"
}
source_yum__ELRepo(){
	rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
echo -e "[elrepo]
name=ELRepo.org Community Enterprise Linux Repository - el${source_version}
baseurl=http://elrepo.org/linux/elrepo/el${source_version}/\$basearch/
	http://mirrors.coreix.net/elrepo/elrepo/el${source_version}/\$basearch/
	http://mirror.rackspace.com/elrepo/elrepo/el${source_version}/\$basearch/
	http://linux-mirrors.fnal.gov/linux/elrepo/elrepo/el${source_version}/\$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo.el${source_version}
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0" > /etc/yum.repos.d/elrepo.repo
	makecache "ELRepo源获取"
}
source_yum__GhettoForge(){
	rpm -Uvh http://mirror.ghettoforge.org/distributions/gf/gf-release-latest.gf.el${source_version}.noarch.rpm
	makecache "GhettoForge源获取"
}
source_yum__Psychotic_Ninja(){
	rpm --import http://wiki.psychotic.ninja/RPM-GPG-KEY-psychotic
	rpm -Uvh http://packages.psychotic.ninja/${source_version}/base/`uname -m`/RPMS/psychotic-release-1.0.0-1.el${source_version}.psychotic.noarch.rpm
	makecache "Psychotic_Ninja源获取"
}
source_yum__IUS_Community(){
	rpm -Uvh https://centos${source_version}.iuscommunity.org/ius-release.rpm
	makecache "IUS_Community源获取"
}
source_yum__NUX_dextop(){
	rpm -Uvh http://li.nux.ro/download/nux/dextop/el${source_version}/x86_64/nux-dextop-release-0-5.el${source_version}.nux.noarch.rpm
	makecache "NUX_dextop源获取"
}
source_yum_openstack_queens(){
	echo '[centos-openstack-queens]
name=CentOS-7 - OpenStack queens
baseurl=http://mirrors.tuna.tsinghua.edu.cn/$contentdir/$releasever/cloud/$basearch/openstack-queens/
gpgcheck=0
enabled=1' > /etc/yum.repos.d/tsinghua-OpenStack-queens.repo
	makecache "centos-openstack-queens源获取"
}
source_yum__nginx(){
	echo '[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true' > /etc/yum.repos.d/nginx.repo
}